#include "serialport.h"
#include "ui_serialport.h"

#include <QDebug>

SerialPort::SerialPort(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SerialPort)
{
    ui->setupUi(this);

    ui->te_recv_text->append(tr("检测到端口列表:"));
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        QSerialPort serial_port;
        serial_port.setPort(info);
        if (serial_port.open(QIODevice::ReadWrite))
        {
            ui->cb_com_port->addItem(info.portName());
            ui->te_recv_text->append(info.portName());
            ui->te_recv_text->append(info.description());
            serial_port.close();
        }
    }

    ui->cb_baud_rate->setCurrentIndex(7);//默认115200
    ui->pb_open_com->setText(tr("打开串口"));
    ui->pb_send->setEnabled(false);
}

SerialPort::~SerialPort()
{
    delete ui;
}

void SerialPort::slotReadUsart(void)
{
    QByteArray requestData = m_serialport->readAll();

    qDebug() << "Recv: " << QString::fromLatin1(requestData);
    ui->te_recv_text->append(tr(requestData));
}

void SerialPort::on_pb_send_clicked()
{
    QString msg = ui->le_send_text->text();
    qDebug() << "Send: " << msg;
    m_serialport->write(msg.toLatin1());
}

void SerialPort::on_pb_clear_clicked()
{
    ui->te_recv_text->clear();
}

void SerialPort::on_pb_open_com_clicked()
{
    if(ui->pb_open_com->text()==tr("打开串口"))
    {
        m_serialport= new QSerialPort();
        m_serialport->setPortName(ui->cb_com_port->currentText());
        m_serialport->open(QIODevice::ReadWrite);
        m_serialport->setBaudRate(ui->cb_baud_rate->currentText().toInt());

        switch(ui->cb_crc_test->currentIndex())
        {
        case 0: m_serialport->setParity(QSerialPort::NoParity);break;
        case 1: m_serialport->setParity(QSerialPort::OddParity);break;
        case 2: m_serialport->setParity(QSerialPort::EvenParity);break;
        default: break;
        }

        switch(ui->cb_data_bit->currentIndex())
        {
        case 0: m_serialport->setDataBits(QSerialPort::Data8);break;
        case 1: m_serialport->setDataBits(QSerialPort::Data7);break;
        case 2: m_serialport->setDataBits(QSerialPort::Data6);break;
        default: break;
        }

        switch(ui->cb_stop_bit->currentIndex())
        {
        case 0: m_serialport->setStopBits(QSerialPort::OneStop);break;
        case 1: m_serialport->setStopBits(QSerialPort::TwoStop);break;
        default: break;
        }

        m_serialport->setFlowControl(QSerialPort::NoFlowControl);

        connect(m_serialport,SIGNAL(readyRead()),
                this,SLOT(slotReadUsart()));

        ui->cb_com_port->setEnabled(false);
        ui->cb_baud_rate->setEnabled(false);
        ui->cb_crc_test->setEnabled(false);
        ui->cb_data_bit->setEnabled(false);
        ui->cb_stop_bit->setEnabled(false);
        ui->lb_statu_flag->setStyleSheet("background-color:red");
        ui->pb_open_com->setText(tr("关闭串口"));
        ui->pb_send->setEnabled(true);
    }else
    {
        m_serialport->clear();
        m_serialport->deleteLater();
        ui->cb_com_port->setEnabled(true);
        ui->cb_baud_rate->setEnabled(true);
        ui->cb_crc_test->setEnabled(true);
        ui->cb_data_bit->setEnabled(true);
        ui->lb_statu_flag->setStyleSheet("background-color:rgb(130,130,130)");
        ui->pb_open_com->setText(tr("打开串口"));
        ui->pb_send->setEnabled(false);
    }
}

void SerialPort::on_pb_beep_clicked()
{
    if(ui->pb_beep->text() == "Beep On")
    {
        m_serialport->write("K");
        ui->pb_beep->setText("Beep Off");
    }else
    {
        m_serialport->write("w");
        ui->pb_beep->setText("Beep On");
    }
}
